/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: erfea (Edvard)
 *    E-Mail: erfea.tux@gmail.com
 */
#include "tspl_se_std_engine.h"

#include <QtScript>

Tspl_se_std_engine::Tspl_se_std_engine(const QString &name, const QString &code, QObject *rootWidget, QObject *parent) :
		QObject(parent),mName(name),mRootWidget(rootWidget)
{
	mEng = new QScriptEngine();
	QObject::connect(mEng,SIGNAL(destroyed()),this,SLOT(deleteLater()));
	QScriptValue self = mEng->newQObject(this);
	mEng->globalObject().setProperty("tsSE",self);
	QScriptValue rWnd = mEng->newQObject(mRootWidget);
	mEng->globalObject().setProperty("tsRootObj",rWnd);
	mEng->evaluate(code);
}

void Tspl_se_std_engine::slotQSLQuery(const QString &name, const QList<QVariant> &params)
{
	emit this->sigAddQuery(name,params,this);
}

void Tspl_se_std_engine::sendRow(QList<QVariant> &row)
{
	emit this->sigSQLData(row);
}

void Tspl_se_std_engine::sendNumRowsAffected(int num)
{
	emit this->sigQSLNumRowsAffected(num);
}

void Tspl_se_std_engine::sendError(QString &errtext)
{
	emit this->sigSQLError(errtext);
}

void Tspl_se_std_engine::slotExit()
{
	mEng->abortEvaluation();
	mEng->deleteLater();
}

bool Tspl_se_std_engine::loadExtension(const QString &name)
{
	if (mEng->availableExtensions().contains(name) && !mEng->importedExtensions().contains(name)){
		mEng->importExtension(name);
		return true;
	}
	return false;
}
